package com.ygqh.baby.utils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.core.convert.converter.Converter;

public class DateConverter implements Converter<String, Date> {
	
	private final List<String> patterns;
	
	public DateConverter() {
		patterns = new ArrayList<String>();
		patterns.add("yyyy/MM/dd");
		patterns.add("yyyy-MM-dd");
		patterns.add("yyyy-MM-dd HH:mm");
		patterns.add("yyyy-MM-dd HH:mm:ss");
		patterns.add("yyyy-MM-dd HH:mm:ss.SSS");
	}

	@Override
	public Date convert(String source) {
		try {
			if (StringUtils.isNotBlank(source)) {
				return DateUtils.parseDate(source, patterns.toArray(new String[]{}));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

}
